package com.zwx.springboot.generator;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Mybatis逆向工程生成类
 */
public class MybatisGenerator {

    public static void main(String[] args)throws Exception{
        String today = "2019-07-15";

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date now = sdf.parse(today);

        Date d = new Date();

        if(d.getTime() > now.getTime() + 1000 * 60 * 60 *24){
            System.err.print("----未成功运行----");
            System.err.print("本程序具有破坏作用 应该只运行一次 如果必须要再运行 需要修改today变量为今天 如 "+sdf.format(new Date()));
            return;
        }

        if(false){
            return;
        }

        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        InputStream is = MybatisGenerator.class.getClassLoader().getResource("generatorConfig.xml").openStream();
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(is);
        is.close();
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);

        System.out.print("生成代码成功  只能执行一次  以后执行方法会覆盖mapper pojo xml 等文件上做的修改");






    }
}
